import {
    WorkLike,
    WorkStar,
    ExamcaseLike,
    ExamcaseStar,
    SetTaskScore,
    SignIn,
} from "~/api";

export const useMobile = () => {
    const userStore = useUserStore();

    const mobile = computed(() => {
        let mobile = userStore.userInfo.mobile;
        return mobile ? mobile.slice(0, 3) + "****" + mobile.slice(7) : "";
    });

    return mobile;
};

export const useLike = () => {
    const userStore = useUserStore();
    const onLike = async (item) => {
        if (userStore.isLogin) {
            const res = await WorkLike({
                wid: item.id,
            });
            if (res.code == 200) {
                item.like = res.data.count;
                item.is_like = res.data.is_like;
                if (res.data.is_like) {
                    SetTaskScore({
                        code: 1005,
                        _id: item.id,
                    });
                }
                GET_MIDNUM();
            } else {
                Message().error("操作失败");
            }
        } else {
            LoginModal();
        }
    };

    return { onLike };
};

export const useStar = () => {
    const userStore = useUserStore();
    const onStar = async (item) => {
        if (userStore.isLogin) {
            const res = await WorkStar({
                wid: item.id,
            });
            if (res.code == 200) {
                item.star = res.data.count;
                item.is_star = res.data.is_star;
                if (res.data.is_star) {
                    SetTaskScore({
                        code: 1006,
                        _id: item.id,
                    });
                }
                GET_MIDNUM();
            } else {
                Message().error("操作失败");
            }
        } else {
            LoginModal();
        }
    };

    return { onStar };
};

export const useSignIn = () => {
    const userStore = useUserStore();
    const dialogStore = useDialogStore();
    return async () => {
        if (userStore.isLogin) {
            await SignIn();
            dialogStore.isSignInDialogVisible = true;
        } else {
            LoginModal();
        }
    };
};

export const useCaseLike = () => {
    const userStore = useUserStore();
    const onLike = async (item) => {
        if (userStore.isLogin) {
            const res = await ExamcaseLike({
                cid: item.id,
            });
            if (res.code == 200) {
                item.like = res.data.count;
                item.is_like = res.data.is_like;
                if (res.data.is_like) {
                    SetTaskScore({
                        code: 1014,
                        _id: item.id,
                    });
                }
                GET_MIDNUM();
            } else {
                Message().error("操作失败");
            }
        } else {
            LoginModal();
        }
    };

    return { onLike };
};

export const useCaseStar = () => {
    const userStore = useUserStore();
    const onStar = async (item) => {
        if (userStore.isLogin) {
            const res = await ExamcaseStar({
                cid: item.id,
            });
            if (res.code == 200) {
                item.star = res.data.count;
                item.is_star = res.data.is_star;
                if (res.data.is_star) {
                    SetTaskScore({
                        code: 1015,
                        _id: item.id,
                    });
                }
                GET_MIDNUM();
            } else {
                Message().error("操作失败");
            }
        } else {
            LoginModal();
        }
    };

    return { onStar };
};
